package table.models;

import java.awt.Component;
import java.awt.event.KeyEvent;

import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableModel;

public class TableWithOverwrite extends JTable {
	public final static String EXCLUDE = "F2";

	private boolean isBlankEditor = false;

	public TableWithOverwrite() {
		super();

	}

	public TableWithOverwrite(TableModel tm) {
		super(tm);
	}

	@Override
	public Component prepareEditor(TableCellEditor editor, int row, int column) {
		Component c = super.prepareEditor(editor, row, column);

		if (this.isBlankEditor) {
			((JTextField) c).setText("");
		}

		return c;
	}

	@Override
	protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
		if (!EXCLUDE.equals(KeyEvent.getKeyText(e.getKeyCode()))) {
			this.isBlankEditor = true;
		}

		boolean retValue = super.processKeyBinding(ks, e, condition, pressed);

		this.isBlankEditor = false;
		return retValue;
	}
}
